import {
    cookie as LsyCookie
} from 'lsy-storages';

export const setToken = (token) => {
    LsyCookie.setItem('token', token, 864000, '/')
}


export const getToken = () => {
    const token = LsyCookie.getItem('token')
    if (token) return token
    else return false
}

export const getUserInfo = () => {
    const info = LsyCookie.getItem('user_info')
    return info ? JSON.parse(decodeURIComponent(info)) : undefined
}


export const getConfig = () => {
    const info = LsyCookie.getItem('config')
    return info ? JSON.parse(decodeURIComponent(info)) : undefined
}


export const getLevelConfig = () => {
    const info = LsyCookie.getItem('level_config')
    return info ? JSON.parse(decodeURIComponent(info)) : undefined
}

export const setDefaultAddress = (data) => {
    return LsyCookie.setItem('default_address', JSON.stringify(data) )
}

export const getDefaultAddress = () => {
    const info = LsyCookie.getItem('default_address')
    return info ? JSON.parse(decodeURIComponent(info)) : undefined
}

export const getCanDrawGoodsWeight = () => {
    const canDrawGoodsWeight = LsyCookie.getItem('can_draw_goods_weight')
    return canDrawGoodsWeight ? canDrawGoodsWeight : 0
}

export const setCanDrawGoodsWeight = (data) => {
    return LsyCookie.setItem('can_draw_goods_weight', data )
}

export const set = (item, value) => {
    return LsyCookie.setItem(item, value, 864000, '/')
}

export const get = (item, defaultV = undefined) => {
    return LsyCookie.getItem(item) || defaultV
}

export const del = (item) => {
    return LsyCookie.setItem(item,'', 1, '/')
}

export const getKeywords = () => {
    var words = LsyCookie.getItem('search_words')
    if(words) {
        return typeof words == 'object' ? words : JSON.parse(words)
    }
    return []
}

export const addKeywords = (word) => {
    var words = getKeywords()
    if(words.indexOf(word)>-1){
        return true
    }
    words.push(word)
    return LsyCookie.setItem('search_words', JSON.stringify(words))
}

export const getCanPickCoupon = () => {
    const info = LsyCookie.getItem('can_pick_coupon')
    return info ? JSON.parse(decodeURIComponent(info)) : undefined
}

export default {
    set,
    get,
    del,
    setToken,
    getToken,
    getUserInfo,
    getConfig,
    getLevelConfig,
    setDefaultAddress,
    getDefaultAddress,
    addKeywords,
    getKeywords,
    setCanDrawGoodsWeight,
    getCanDrawGoodsWeight,
    getCanPickCoupon
}